import sys
import time

from PyQt5.QtCore import Qt, pyqtSignal, QFile
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QLabel, QScrollArea, QPushButton, QHBoxLayout, QWidget, \
    QPlainTextEdit, QFileDialog, QFormLayout, QLineEdit, QGridLayout

from thread_多线程.file_uploader import FileUploader
from thread_多线程.qthread import SignalThread
from thread_多线程.signalEmitter import SignalEmitter


# 主线程负责窗口的展示
class MyWindow(QWidget):

    def __init__(self):
        super().__init__()
        # 创建一个信号发射者
        self.emitter = SignalEmitter()
        # 创建一个线程,将信号发送者给他
        self.signal_thread = SignalThread(self.emitter, 2000)
        self.setWindowTitle("王多鱼web外挂 (qq:289024114)")
        self.resize(600, 200)
        self.init_ui()

    def select_file(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file_name, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "所有文件 (*);;Python 文件 (*.py)",
                                                   options=options)
        print(file_name)
        self.file_name = file_name
        pass

    def init_ui(self):
        # 整体布局是垂直布局器
        self.container = QVBoxLayout()

        # 文件上传按钮
        self.upload_file = QPushButton('上传文件', self)
        self.upload_file.clicked.connect(self.select_file)
        self.container.addWidget(self.upload_file)
        # 创建一个表单布局器
        # self.form_layout = QFormLayout()
        self.grid_layout = QGridLayout()


        proxyHost = QLineEdit()
        proxyHost.setFixedWidth(100)
        proxyHost.setAlignment(Qt.AlignLeft)
        proxyPort = QLineEdit()
        proxyPort.setFixedWidth(40)
        proxyUser = QLineEdit()
        proxyPassword = QLineEdit()
        proxyType = QLineEdit()

        labels = {
            0: ["代理服务器地址", "端口", "协议类型"],
            1: ["用户名", "密码"],
        }
        for i in range(len(labels)):
            #添加标签
            self.grid_layout.addWidget(QLabel(labels[i][0]), i, 0)
            #添加输入框
            self.grid_layout.addWidget(proxyHost, i, 1)
            # 添加标签
            self.grid_layout.addWidget(QLabel(labels[i][1]), i, 2)
            # 添加输入框
            self.grid_layout.addWidget(proxyPort, i, 3)


        self.container.addLayout(self.grid_layout)

        # 创建一个文本框
        self.text_edit = QPlainTextEdit(self)
        # 禁止用户编辑
        self.text_edit.setReadOnly(True)
        self.text_edit.resize(610, 200)
        # 关闭水平放线的滚动条
        self.text_edit.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        # 关闭垂直滚动条
        self.text_edit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        # 创建一个垂直布局器 把QPlainTextEdit 放到这个垂直布局器中
        self.qb = QVBoxLayout()
        self.qb.addWidget(self.text_edit)

        # 开始按钮
        self.btn1 = QPushButton("运行")
        self.btn1.setFixedSize(80, 30)
        # 停止按钮
        self.stop_btn = QPushButton("停止")
        self.stop_btn.setFixedSize(80, 30)
        self.stop_btn.hide()

        # 给按钮绑定槽函数
        self.btn1.clicked.connect(self.btn_click)
        self.stop_btn.clicked.connect(self.stop_btn_click)
        self.emitter.customSignal.connect(self.print_hello)

        # 创建一个水平布局器 用来存放按钮
        self.hb = QHBoxLayout()
        self.hb.addStretch(1)
        self.hb.addWidget(self.btn1)
        self.hb.addWidget(self.stop_btn)
        self.hb.addStretch(1)

        # 垂直布局器 水平布局器 设置到整体的窗口布局中
        self.container.addLayout(self.qb)
        self.container.addLayout(self.hb)
        self.setLayout(self.container)
        pass

    def stop_btn_click(self):
        # self.signal_thread.terminate() 这种会抛出一个SystemExits异常,可以在子线程中捕获该异常,从而可以安全的结束线程
        self.signal_thread.ifStop = True
        self.stop_btn.hide()
        self.btn1.show()

    def btn_click(self):
        # 执行work线程 将结果发送给 self customSignal
        self.ToggleBt()

    def ToggleBt(self):
        if self.btn1.isVisible():
            self.signal_thread.ifStop = False
            self.btn1.hide()
            self.stop_btn.show()
            self.signal_thread.set_excel_file(self.file_name)
            self.signal_thread.start()
        else:
            self.signal_thread.ifStop = False
            self.btn1.show()
            self.stop_btn.show()

    def print_hello(self, msg):
        print(msg)
        current_text = self.text_edit.toPlainText()
        # 添加新文本
        new_text = current_text + f'\n{msg}'
        # 设置新文本
        self.text_edit.setPlainText(new_text)
        self.text_edit.repaint()
        # 让右侧滚动条始终在底部
        self.text_edit.verticalScrollBar().setValue(self.text_edit.verticalScrollBar().maximum())


#
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())
